from PyQt4.QtGui import * 
from PyQt4.QtCore import * 

from tucklist import *

class LayersDialog2(TuckList):
    def __init__(self, editor):
        self.editor = editor
        TuckList.__init__(self, "Layers", self.editor, True)

    def initialize_buttons(self):
        self.add_button("add_layer", "+")
        self.add_button("del_layer", "-")
        self.add_button("move_layer_up", "Up")
        self.add_button("move_layer_down", "Down")
        self.add_button("merge_layer_down", "Merge")
        self.add_button("copy_to_all_frames", "To All")

    def row_changed(self, ev):
        if self.list.currentRow() >= 0:
	        self.editor.set_selected_layer(self.list.currentRow())

    def move_layer_up(self):
        index = self.editor.get_selected_frame().move_layer_up(self.list.currentRow())
        if index != None:
            self.initialize_list()
            self.editor.update()
            self.list.setCurrentRow(index)

    def move_layer_down(self):
        index = self.editor.get_selected_frame().move_layer_down(self.list.currentRow())
        if index != None:
            self.initialize_list()
            self.editor.update()
            self.list.setCurrentRow(index)

    def merge_layer_down(self):
        index = self.editor.get_selected_frame().merge_layer_down(self.list.currentRow())
        if index != None:
            self.initialize_list()
            self.editor.update()
            self.list.setCurrentRow(index)

    def copy_to_all_frames(self):
        sprite = self.editor.sprite
        frame = self.editor.get_selected_frame()
        layer_index = self.list.currentRow()

        sprite.copy_layer_to_all_frames(frame, layer_index)
        
    def row_changed(self, ev):
        if self.list.currentRow() >= 0:
	        self.editor.set_selected_layer(self.list.currentRow())
        
    def add_layer(self):
        for frame in self.editor.sprite.frames:
	        index = frame.add_empty_layer()
        self.initialize_list()
        self.editor.update()
        self.list.setCurrentRow(index)
        
    def del_layer(self):
        for frame in self.editor.sprite.frames:
	        if len(frame.layers) > 1:
		        frame.delete_layer(frame.layers[self.list.currentRow()])
		        self.list.setCurrentRow(len(frame.layers))
        self.initialize_list()
        self.editor.update()

    def get_items(self):
        return self.editor.get_selected_frame().layers

    def get_item_name(self, layer):
        return "Layer " + str(self.editor.get_selected_frame().layers.index(layer) + 1)
